package csu.web.credit_bank.controller;


import csu.web.credit_bank.pojo.ShopItem;
import csu.web.credit_bank.pojo.vo.UserShopItem;
import csu.web.credit_bank.service.ShopItemService;
import csu.web.credit_bank.utils.AliOssUtil;
import csu.web.credit_bank.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
public class CreditShopController {
    @Autowired
    private ShopItemService shopItemService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @PostMapping("/shop_items")
    public Result addShopItem(
            @RequestParam("title") String title,
            @RequestParam("type") int type,
            @RequestParam("price") int price,
            @RequestParam("description") String description,
            @RequestParam("inventory") int inventory,
            @RequestPart(value = "image", required = false) MultipartFile image) {
        ShopItem shopItem=new ShopItem();
        shopItem.setTitle(title);
        shopItem.setType(type);
        shopItem.setPrice(price);
        shopItem.setDescription(description);
        shopItem.setInventory(inventory);
        return shopItemService.addNewShopItem(shopItem,image);
    }
    @GetMapping("/shop_items")
    public Result getShopItems() {
        return shopItemService.getShopItems();
    }
    @DeleteMapping("/shop_items/{id}")
    public Result shopItem(@PathVariable String id,@RequestBody UserShopItem userShopItem) {
         return shopItemService.shopItem(userShopItem,id);
    }
    @GetMapping("/item_exchanges/{id}")
    public Result getItemExchanges(@PathVariable String id) {
        return shopItemService.getItemExchanges(id);
    }
    @PutMapping("/item_exchanges/{id}")
    public Result updateItemExchange(@PathVariable String id,@RequestBody UserShopItem userShopItem) {
        return shopItemService.updateItemExchange(userShopItem,id);
    }
}
